package com.zy.leaderSide.Controler;

import com.zy.leaderSide.entity.Leader;
import com.zy.leaderSide.service.ILeaderService;
import com.zy.util.R;
import com.zy.util.RandomNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangdihua
 * @date 2021/12/27 10:21
 */
@RestController
public class LeaderController {

    @Autowired
    private ILeaderService ils;

    /**
     * 领导登录
     *
     * @param leader
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/leaderLogin")
    public R leaderLogin(@RequestBody Leader leader) {
        Leader lea = ils.leaderLogin(leader.getLeaderNum(), leader.getLeaderPassword());
        return new R(lea == null ? false : true, lea);
    }

    /**
     * 领导注册接口
     *
     * @param leader
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/leaderRegister")
    public R leaderRegister(@RequestBody Leader leader) {

        if (ils.getLeaderById(leader.getLeaderNum()) != null) {
            return new R(false, "err");
        } else {
            Integer id = RandomNum.getRandomID(6);
            leader.setLeaderId(id);
            leader.setLeaderShiro(3);
            return new R(ils.save(leader), leader);
        }
    }
}
